package com.wzdigit.wms.raw.config.redisson;

import org.redisson.Redisson;
import org.redisson.config.ClusterServersConfig;
import org.redisson.config.Config;
import org.redisson.config.SingleServerConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.List;

@Configuration
public class RedissonConfig {
    @Autowired
    private RedisConfigProperties redisConfigProperties;

    //添加redisson的bean
    @Bean
    public Redisson redisson() {
        //redisson版本是3.5，集群的ip前面要加上“redis://”，不然会报错，3.2版本可不加
        List<String> clusterNodes = new ArrayList<>();
        for (int i = 0; i < redisConfigProperties.getCluster().getNodes().size(); i++) {
            clusterNodes.add(redisConfigProperties.getCluster().getNodes().get(i));
        }
        Config config = new Config();

        if (clusterNodes.size() > 1) {
            ClusterServersConfig clusterServersConfig = config.useClusterServers()
                    .addNodeAddress(clusterNodes.toArray(new String[clusterNodes.size()]));
            if (redisConfigProperties.getPassword() != null && !"".equals(redisConfigProperties.getPassword())) {
                clusterServersConfig.setPassword(redisConfigProperties.getPassword());//设置密码
            }
            clusterServersConfig.setTimeout(redisConfigProperties.getTimeout());
        } else {
            SingleServerConfig singleServerConfig = config.useSingleServer();
            singleServerConfig.setAddress(clusterNodes.get(0));
            if (redisConfigProperties.getPassword() != null && !"".equals(redisConfigProperties.getPassword())) {
                singleServerConfig.setPassword(redisConfigProperties.getPassword());
            }
            singleServerConfig.setTimeout(redisConfigProperties.getTimeout());
            singleServerConfig.setDatabase(redisConfigProperties.getDatabase());
        }
        return (Redisson) Redisson.create(config);
    }
}
